<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"anynone.gitee.io","root":"/","images":"/images","scheme":"Pisces","darkmode":true,"version":"8.7.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="iptables规则设置完成后重启会丢失,有两种方式可以实现永久生效 网卡启动前执行脚本,重新配置开发端口 iptables-restore，iptables-save完成规则保存和重载   网卡启动配置增加iptables脚本方式 centosnetwork-scripts&#x2F;etc&#x2F;sysconfig&#x2F;network-scripts&#x2F;ifup-post 增加iptables命令即可 1echo">
<meta property="og:type" content="article">
<meta property="og:title" content="centos&#x2F;ubuntu iptables持久生效">
<meta property="og:url" content="https://anynone.gitee.io/2021/09/27/iptables-always-md/index.html">
<meta property="og:site_name" content="anynone">
<meta property="og:description" content="iptables规则设置完成后重启会丢失,有两种方式可以实现永久生效 网卡启动前执行脚本,重新配置开发端口 iptables-restore，iptables-save完成规则保存和重载   网卡启动配置增加iptables脚本方式 centosnetwork-scripts&#x2F;etc&#x2F;sysconfig&#x2F;network-scripts&#x2F;ifup-post 增加iptables命令即可 1echo">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-09-27T14:31:29.000Z">
<meta property="article:modified_time" content="2022-09-09T00:09:56.415Z">
<meta property="article:author" content="山丘之王">
<meta property="article:tag" content="iptabels,linux">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://anynone.gitee.io/2021/09/27/iptables-always-md/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://anynone.gitee.io/2021/09/27/iptables-always-md/","path":"2021/09/27/iptables-always-md/","title":"centos/ubuntu iptables持久生效"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>centos/ubuntu iptables持久生效 | anynone</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">anynone</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <img src="/images/head.jpg">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-5"><a class="nav-link" href="#iptables%E8%A7%84%E5%88%99%E8%AE%BE%E7%BD%AE%E5%AE%8C%E6%88%90%E5%90%8E%E9%87%8D%E5%90%AF%E4%BC%9A%E4%B8%A2%E5%A4%B1-%E6%9C%89%E4%B8%A4%E7%A7%8D%E6%96%B9%E5%BC%8F%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E6%B0%B8%E4%B9%85%E7%94%9F%E6%95%88"><span class="nav-number">1.</span> <span class="nav-text">iptables规则设置完成后重启会丢失,有两种方式可以实现永久生效</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E7%BD%91%E5%8D%A1%E5%90%AF%E5%8A%A8%E9%85%8D%E7%BD%AE%E5%A2%9E%E5%8A%A0iptables%E8%84%9A%E6%9C%AC%E6%96%B9%E5%BC%8F"><span class="nav-number">2.</span> <span class="nav-text">网卡启动配置增加iptables脚本方式</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#iptables-restore-iptables-save%E6%96%B9%E5%BC%8F"><span class="nav-number">3.</span> <span class="nav-text">iptables-restore, iptables-save方式</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">山丘之王</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">22</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/anynone" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;anynone" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://anynone.gitee.io/2021/09/27/iptables-always-md/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="山丘之王">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="anynone">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          centos/ubuntu iptables持久生效
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-09-27 22:31:29" itemprop="dateCreated datePublished" datetime="2021-09-27T22:31:29+08:00">2021-09-27</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-09-09 08:09:56" itemprop="dateModified" datetime="2022-09-09T08:09:56+08:00">2022-09-09</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <blockquote>
<h5 id="iptables规则设置完成后重启会丢失-有两种方式可以实现永久生效"><a href="#iptables规则设置完成后重启会丢失-有两种方式可以实现永久生效" class="headerlink" title="iptables规则设置完成后重启会丢失,有两种方式可以实现永久生效"></a>iptables规则设置完成后重启会丢失,有两种方式可以实现永久生效</h5><ol>
<li>网卡启动前执行脚本,重新配置开发端口</li>
<li>iptables-restore，iptables-save完成规则保存和重载</li>
</ol>
</blockquote>
<h5 id="网卡启动配置增加iptables脚本方式"><a href="#网卡启动配置增加iptables脚本方式" class="headerlink" title="网卡启动配置增加iptables脚本方式"></a>网卡启动配置增加iptables脚本方式</h5><ul>
<li><p>centos<br>network-scripts/etc/sysconfig/network-scripts/ifup-post 增加iptables命令即可</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">echo</span> <span class="string">&quot;iptables -I INPUT -p tcp --dport 8080 -j ACCEPT&quot;</span> &gt;&gt; network-scripts/etc/sysconfig/network-scripts/ifup-post</span><br></pre></td></tr></table></figure></li>
<li><p>ubuntu/debian<br>/etc/network/if-up.d/* 建立配置文件增加脚本,给执行权限</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">echo</span> <span class="string">&quot;iptables -I INPUT -p tcp --dport 8080 -j ACCEPT&quot;</span> &gt; /etc/network/if-up.d/test_shell</span><br><span class="line">chmod +x /etc/network/if-up.d/test_shell</span><br></pre></td></tr></table></figure>
<h5 id="iptables-restore-iptables-save方式"><a href="#iptables-restore-iptables-save方式" class="headerlink" title="iptables-restore, iptables-save方式"></a>iptables-restore, iptables-save方式</h5></li>
<li><p>保存规则命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">iptables-save &gt; /etc/iptables.save.rules</span><br></pre></td></tr></table></figure></li>
<li><p>加载规则命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">iptables-restore &lt; /etc/iptables.save.rules</span><br></pre></td></tr></table></figure></li>
<li><p>规则保存和重载命令放到 <code>/etc/network/interfaces</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">echo</span> <span class="string">&quot;pre-down iptables-save &gt; /etc/iptables.save.rules&quot;</span> <span class="comment">## 网卡关闭前保存规则</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;pre-up iptables-restore &lt; /etc/iptables.save.rules&quot;</span> <span class="comment">## 网卡启动前重载规则 </span></span><br></pre></td></tr></table></figure></li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/iptabels-linux/" rel="tag"># iptabels,linux</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/09/27/php-kylin-install-md/" rel="prev" title="麒麟系统安装nginx-php7.2.32">
                  <i class="fa fa-chevron-left"></i> 麒麟系统安装nginx-php7.2.32
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/09/27/elasticsearch-install-kylin-md/" rel="next" title="centos/kylin(yum)安装elasticsearch6.3.1">
                  centos/kylin(yum)安装elasticsearch6.3.1 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2021 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">山丘之王</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
